package com.turboconnard.display;

import javax.microedition.khronos.opengles.GL10;

import android.graphics.drawable.Drawable;

import com.turboconnard.data.Library;
import com.turboconnard.tools.Textures;

public class Animation extends Plane {
	public int[] frames;
	private float count;
	private int _currentFrame;
	public float frameRate = 60;
	
	public Animation(int[] pFrames,int pAlign){
		super();
		frames = pFrames;
		count = 0;
		Drawable v = Library.getInstance().resources.getDrawable(pFrames[0]);
		height = v.getIntrinsicHeight()*stage.correction/2;
		width = v.getIntrinsicWidth()*stage.correction/2;
		init(pAlign);
		_currentFrame = frames[(int)count];
	}
	public Animation(int[] pFrames){
		super();
		frames = pFrames;
		count = 0;
		Drawable v = Library.getInstance().resources.getDrawable(pFrames[0]);
		height = v.getIntrinsicHeight()*stage.correction/2;
		width = v.getIntrinsicWidth()*stage.correction/2;
		init(0);
		_currentFrame = frames[(int)count];
	}
	
	@Override
	public void _update(){
		count += frameRate/60;				
		if(count>=frames.length) count=0;
		_currentFrame = frames[(int) Math.floor(count)];
	}
	@Override
	protected void _draw(GL10 gl) {
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
		gl.glFrontFace(GL10.GL_CCW);
		
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
		gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
		int mode;
		if (frames.length != 0) {
			mode = GL10.GL_TRIANGLES;
			gl.glEnable(GL10.GL_TEXTURE_2D);
			gl.glBindTexture(GL10.GL_TEXTURE_2D, Textures.getInstance().getTexture(_currentFrame));
			gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
			mode = GL10.GL_TRIANGLES;
		}else{
			mode = GL10.GL_LINE_LOOP;
		}
		gl.glDrawElements(mode, indices.length, GL10.GL_UNSIGNED_BYTE, indexBuffer);
			
		gl.glDisable(GL10.GL_TEXTURE_2D);
		gl.glDisable(GL10.GL_BLEND);
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
	}
	
}
